package cn.tedu.api;

import java.util.Arrays;
import java.util.Locale;

/*本类用来测试String类的常用方法*/
public class TestString2 {
    public static void main(String[] args) {

    char[] c={'a','b','c'};
    String s1=new String(c);
    String s2="abc";

    /*String重写了hashcode(),是根据子字符串的具体内容生成哈希码值,不是根据地址值
    * 所以s1,s2的地址值不一样,但是哈希码值一样*/
        System.out.println(s1.equals(s2));
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
    /*string也重写了object中的toString,打印的是字符串的具体内容*/
        System.out.println(s1);

        System.out.println("____________________");
        System.out.println(s1.length());
        System.out.println(s1.toUpperCase(Locale.ROOT));
        System.out.println(s1.toUpperCase());
        System.out.println(s2.toLowerCase(Locale.ROOT));
        System.out.println(s2.toLowerCase());

        System.out.println("____________________");
        System.out.println(s1.startsWith("a"));//判断是否以a开头
        System.out.println(s1.endsWith("c"));//是否以c结束
        System.out.println(s1.charAt(0));//a

        System.out.println("____________________");
        String s3="abcbdbba";
        System.out.println(s3.indexOf("b"));//1返回指定字符第一次出现的下标
        System.out.println(s3.lastIndexOf("b"));//4返回指定字符最后一次出现的下标

        System.out.println("____________________");
        System.out.println(s2.concat("cxy"));//abccxy临时保存拼接的
        System.out.println(s2);

        System.out.println("____________________");
        String s4=s2.concat("aaa");
        System.out.println(s4);

        System.out.println("____________________");
        String s5="afbfcfdfe";
        String[] a=s5.split("f");//[a, b, c, d, e]
        //以指定字符作为分隔符,分割当前字符串
        System.out.println(Arrays.toString(a));

        System.out.println("____________________");
        for (int i = 0; i <a.length ; i++) {
            System.out.println(a[i]);
        }

        System.out.println("____________________");
        String s6="     hh  hhh     ";
        System.out.println(s6);
        System.out.println(s6.trim());//去除首尾空格,不改变字符串
        System.out.println(s6);


        System.out.println("____________________");
        String s7="abcdefgh";
        System.out.println(s7.substring(3));//defgh,截取指定下标之后的片段[3,结束]
        System.out.println(s7.substring(3,6));//def,截取指定下标[3,6)

        System.out.println("____________________");
        System.out.println(String.valueOf(10));
        System.out.println(String.valueOf(10)+20);
        System.out.println("20"+10);//将Int类型换成String类型

        System.out.println("____________________");
        byte[] bs=s7.getBytes();
        System.out.println(bs);
        System.out.println(Arrays.toString(bs));

    }
}
